package com.zry.traced.ui.activity

import android.content.Intent
import com.zry.traced.MainActivity
import com.zry.traced.R

import com.zry.traced.databinding.ActivitySubmitPutStorageBinding
import com.zry.traced.greendao.entity.OutTreasuryCode
import com.zry.traced.greendao.entity.PackingOperationInfo
import com.zry.traced.greendao.entity.PutStorageCode
import com.zry.traced.greendao.gen.OutTreasuryCodeDao
import com.zry.traced.greendao.gen.PackingOperationInfoDao
import com.zry.traced.greendao.gen.PutStorageCodeDao
import com.zry.traced.presenter.SubmitPutStoragePresenter
import com.zry.traced.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_submit_put_storage.*
import kotlinx.android.synthetic.main.common_title.*

class SubmitPutStorageActivity : BaseActivity() {
    private var submitPutStorageBinding: ActivitySubmitPutStorageBinding? = null
    private var submitPutStoragePresenter: SubmitPutStoragePresenter? = null

    var title: String = ""
    var timeID: String = ""
    var operationType: String = ""

    var packingOperationInfo: PackingOperationInfo? = null
    var putStorageCode: PutStorageCode? = null
    var outTreasuryCode: OutTreasuryCode? = null


    override fun getLayoutId(): Int {
        return R.layout.activity_submit_put_storage
    }

    override fun initData() {


        commonLeftLinearLayout.setOnClickListener { finish() }

        titleRightImage.setImageResource(R.mipmap.order_delete)

        submitPutStorageBinding = viewDataBinding as ActivitySubmitPutStorageBinding
        submitPutStoragePresenter = SubmitPutStoragePresenter(this)
        submitPutStorageBinding?.handlerClick = submitPutStoragePresenter

        getIntentData()
        submitPutStoragePresenter?.initView()
    }

    private fun getIntentData() {

        title = intent.getStringExtra("title")
        timeID = intent.getStringExtra("TimeID")


        titleRightImage.setOnClickListener {

            when (title) {
                "批量装箱", "号段装箱" -> {
                    packingOperationInfoDao?.deleteByKey(timeID)
                    submitPutStorageBtn.text = "确认装箱"
                    titleCenterText.text = "确认装箱"
                }
                "批量入库", "号段入库" -> {

                    titleCenterText.text = "确认入库"
                    submitPutStorageBtn.text = "确认入库"

                    putStorageCodeDao?.deleteByKey(timeID)
                }
                "批量出库", "号段出库" -> {
                    outTreasuryCodeDao?.deleteByKey(timeID)
                    submitPutStorageBtn.text = "确认出库"
                    titleCenterText.text = "确认出库"
                }
            }

            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }


        when (title) {
            "批量入库", "号段入库" -> {
                titleCenterText.text = "确认入库"
                submitPutStorageBtn.text = "确认入库"
                putStorageCode = putStorageCodeDao!!.queryBuilder().where(PutStorageCodeDao.Properties.TimeID.eq(timeID)).build().unique()
                operationType = putStorageCode!!.operationType
            }
            "批量出库", "号段出库" -> {
                submitPutStorageBtn.text = "确认出库"
                titleCenterText.text = "确认出库"
                outTreasuryCode = outTreasuryCodeDao!!.queryBuilder().where(OutTreasuryCodeDao.Properties.TimeID.eq(timeID)).build().unique()
                operationType = outTreasuryCode!!.operationType
            }

            "批量装箱", "号段装箱" -> {
                submitPutStorageBtn.text = "确认装箱"
                titleCenterText.text = "确认装箱"
                packingOperationInfo = packingOperationInfoDao!!.queryBuilder().where(PackingOperationInfoDao.Properties.TimeID.eq(timeID)).build().unique()
                operationType = packingOperationInfo!!.operationType
            }
        }
    }
}
